﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SStuff.Pivot
{
    [TemplateVisualState(GroupName = "SelectionStates", Name = "Selected")]
    [TemplateVisualState(GroupName = "SelectionStates", Name = "Unselected")]
    public class PivotItem : ContentControl
    {
        private readonly PivotItemsControl _owner;

        protected PivotItem()
        {
            DefaultStyleKey = typeof (PivotItem);
            Loaded += new RoutedEventHandler(OnLoaded);
            Unloaded += new RoutedEventHandler(OnUnloaded);
        }

        public PivotItem(PivotItemsControl owner)
            :this()
        {
            _owner = owner;
        }

        private bool _loaded;
        void OnUnloaded(object sender, RoutedEventArgs e)
        {
            _loaded = false;
        }

        private UIElement _parent;
        private TranslateTransform _parentTransform;

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            UpdateSelectionState();
            _parent = VisualTreeHelper.GetParent(this) as UIElement;
            _parentTransform = _parent.RenderTransform as TranslateTransform;
            if(_parentTransform == null)
            {
                _parentTransform = new TranslateTransform();
                _parent.RenderTransform = _parentTransform;
            }
            if(IsSelected)
            {
                var renderFromParent = _parent.TransformToVisual(this);
                var transformedOrigin = renderFromParent.Transform(new Point(0, 0));
                _parentTransform.X = transformedOrigin.X;
            }
            _loaded = true;
        }

        private void UpdateSelectionState()
        {
            VisualStateManager.GoToState(this, IsSelected ? "Selected" : "Unselected", _loaded);
        }

        protected override void OnManipulationStarted(ManipulationStartedEventArgs e)
        {
            
            e.Handled = true;
            this.CaptureMouse();
            IsSelected = true;
        }

        protected override void OnManipulationDelta(ManipulationDeltaEventArgs e)
        {
            e.Handled = true;
        }

        protected override void OnManipulationCompleted(ManipulationCompletedEventArgs e)
        {
            e.Handled = true;
        }



        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsSelected.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(PivotItem), new PropertyMetadata(false, (s,a)=> ((PivotItem)s).OnIsSelectedChanged()));

        private void OnIsSelectedChanged()
        {

            UpdateSelectionState();

            if(IsSelected && _owner!=null)
            {
                var lastSelectedContainer = _owner.ItemContainerGenerator.ContainerFromItem(_owner.SelectedItem) as PivotItem;
                if(lastSelectedContainer != null && lastSelectedContainer!=this)
                {
                    lastSelectedContainer.IsSelected = false;
                }
                _owner.SelectedItem = this.Content;
            }

        }



        public object Header
        {
            get { return (object)GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Header.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeaderProperty =
            DependencyProperty.Register("Header", typeof(object), typeof(PivotItem), new PropertyMetadata(null));



        public DataTemplate HeaderTemplate
        {
            get { return (DataTemplate)GetValue(HeaderTemplateProperty); }
            set { SetValue(HeaderTemplateProperty, value); }
        }

        // Using a DependencyProperty as the backing store for HeaderTemplate.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeaderTemplateProperty =
            DependencyProperty.Register("HeaderTemplate", typeof(DataTemplate), typeof(PivotItem), new PropertyMetadata(null));


    }
}
